package com.xuwang.service.impl;

import com.xuwang.Util.JWTutil;
import com.xuwang.mapper.*;
import com.xuwang.pojo.Inventory;
import com.xuwang.pojo.Stock;
import com.xuwang.vo.Company01VO;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 进货表（菜品、原料、添加剂都在这里面）(Stock)表服务实现类
 *
 * @author abaaba
 * @since 2021-09-18 14:18:00
 * @version 1.0
 */
@Service("stockService")
public class StockServiceImpl {
    @Resource
    private StockMapper stockMapper;

    @Resource
    private AdditiveMapper additiveMapper;
    @Resource
    private InventoryMapper inventoryMapper;
    @Resource
    private ProductionMapper productionMapper;
    @Resource
    private FoodSampleMapper foodSampleMapper;




//    /**
//     * 根据模糊条件查询总个数
//     *
//     * @param name 查询条件
//     * @return 返回查询到的总个数
//     */
//    public Map<String, Object> selectForCount(String name) {
//        Map<String, Object> map = new HashMap<>();
//        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
//        map.put("code", 200);
//        map.put("msg", "查询成功");
//        map.put("data", this.stockMapper.selectForCount(name));
//        return map;
//    }

    /**
     * 查询所有数据
     * @return  返回所有数据
     */
    public Map<String, Object> selectAll() {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.stockMapper.selectAll());
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public Map<String, Object> selectById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        String batchNo = stockMapper.selectBatchNoById(id);
        Integer type = productionMapper.selectMatchTypeByBatchNo(batchNo);
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.stockMapper.selectById(id));
        map.put("food", this.productionMapper.selectByBatchNo(batchNo,type));
        return map;
    }


    public Map<String, Object> selectAllCompany(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String userName = JWTutil.getUserName(token);
        Map<String, Object> map = new HashMap<>();
//        int companyIdByUsername = additiveMapper.selectCompanyIdByUsername("原料人");
        int companyIdByUsername = additiveMapper.selectCompanyIdByUsername(userName);
        List<Company01VO> company01VOList= additiveMapper.selectCompanyNameAndIdByUsername(companyIdByUsername);


        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data",company01VOList);

        return map;
    }

    /**
     * 查询分页数据
     *
//     * @param index 查询起始位置
//     * @param name  查询条件
     * @return 对象列表
     */
    public Map<String, Object> selectForPage(int m, int index, int page,String name,
                                             String batchNo, String type, String starTime, String endTime, Integer warranty, String supplyCompanyName,
                                             String receiveCompanyName, HttpServletRequest request) {

        String token = request.getHeader("Authorization");
        String userName = JWTutil.getUserName(token);
        if (m==2){
            supplyCompanyName = additiveMapper.selectCompanyNameByUsername(userName);
//            supplyCompanyName = additiveMapper.selectCompanyNameByUsername("原料人");
        }else if(m==3){
            receiveCompanyName=additiveMapper.selectCompanyNameByUsername(userName);
//            receiveCompanyName=additiveMapper.selectCompanyNameByUsername("菜品人");
        }
        if (("1970-01-01 08:00:00").equals(starTime)) {
            starTime="";
        }
        if (("1970-01-01 08:00:00").equals(starTime)) {
            endTime="";
        }
        // 获取当前表中的总记录
        int tableCount = this.stockMapper.selectForCount(m,name,batchNo,type,starTime,endTime,warranty,supplyCompanyName,receiveCompanyName);
        // 总页码计算   (总条数 - 1) / 每页显示条数  + 1
        // (100 - 1) / 10 + 1 = 10        (101 - 1) / 10 + 1 = 11      (99 - 1) / 10 + 1 = 10
        int pageCount = (tableCount - 1) / page + 1;
        // 计算每页开始的下标值
        index = (index - 1) * page;
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "查询成功");
        map.put("pageCount", pageCount);  // 查询的记录总页码
        map.put("count", tableCount);     // 当前表中的总条数
        map.put("data", this.stockMapper.selectForPage(m,index, page,name,batchNo,type,starTime,endTime,warranty,supplyCompanyName,receiveCompanyName));
        return map;
    }

    /**
     * 新增数据
     *
     * @param stock 实例对象
     * @return 实例对象
     */
    public Map<String, Object> insert(Stock stock,int m,HttpServletRequest request) {
        // UUID.randomUUID()  返回内容：asd21321-ewrewrew213213-123213zsad-123asdasd这样的形态
        Map<String, Object> map = new HashMap<>();
        Integer addCompanyId = 0;
        Integer subCompanyId = additiveMapper.selectCompanyIdByBatchNo(stock.getStockBatchNo());
        Integer type = additiveMapper.selectTypeByBatchNo(stock.getStockBatchNo());
        String name = additiveMapper.selectMatchNameByBatchNo(stock.getStockBatchNo());
        stock.setStockTime( Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
        if (m == 1) {
            String token = request.getHeader("Authorization");
            String userName = JWTutil.getUserName(token);
            addCompanyId = additiveMapper.selectCompanyIdByUsername(userName);
//            addCompanyId = additiveMapper.selectCompanyIdByUsername("菜品人");
            stock.setStockCompanyId(addCompanyId);

        } else if (m == 2) {
            addCompanyId = stock.getStockCompanyId();
        }

        //        库存减少的公司
        Inventory inventory1 = inventoryMapper.selectIdAndNumberByNoAndCompanyId(stock.getStockBatchNo(), subCompanyId);//插入数据到库存中
        map.put("code", 201);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "供货方库存不足，请提醒供货方生产");
        if (inventory1!=null){
        if (inventory1.getNumber()>stock.getStockNumber()) {
            inventoryMapper.updateNumberByNoAndCompanyId(inventory1.getNumber() - stock.getStockNumber(), stock.getStockBatchNo(), subCompanyId);

//        库存增加的公司
            Inventory inventory = inventoryMapper.selectIdAndNumberByNoAndCompanyId(stock.getStockBatchNo(), addCompanyId);//插入数据到库存中
            if (inventory == null) {
                //插入库存
                this.inventoryMapper.insert(type, stock.getStockBatchNo(), name, stock.getStockNumber(), addCompanyId);
            } else {
                //更改库存
                inventoryMapper.updateNumberByNoAndCompanyId(stock.getStockNumber() + inventory.getNumber(), stock.getStockBatchNo(), addCompanyId);
            }



            this.stockMapper.insert(stock);

            map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
            map.put("msg", "新增成功");
        }}
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param stock 实例对象
     * @return 实例对象
     */
    public Map<String, Object> updateById(Stock stock) {
        this.stockMapper.updateById(stock);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "更新成功");
        return map;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public Map<String, Object> deleteById(String id) {
        this.stockMapper.deleteById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "删除成功");
        return map;
    }
}